package xyz.yuzegod.megawalls.timer;

import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GameParty;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.scoreboard.FixedBody;
import xyz.yuzegod.megawalls.scoreboard.SidebarBoard;
import xyz.yuzegod.megawalls.scoreboard.TextLine;
import xyz.yuzegod.megawalls.stats.EffectStatsContainer;

import java.text.SimpleDateFormat;
import java.util.*;

public class ScoreBoardTimer implements Runnable {
    public static final Map<GamePlayer, SidebarBoard> scoreboards = new HashMap<>();
    private Game game;
    private String WAIT_TITLE = "§e§l超级战墙";
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy §8HH:mm");

    public ScoreBoardTimer(Game game) {
        this.game = game;
    }

    public void run() {
        if (this.game != null) {

            for (GamePlayer gamePlayer : GamePlayer.getOnlinePlayers()) {
                SidebarBoard b;
                if (!scoreboards.containsKey(gamePlayer)) {
                    b = SidebarBoard.of(MegaWalls.getInstance(), (Player) gamePlayer.getPlayer());
                    gamePlayer.getPlayer().setScoreboard(b.getScoreboard());
                    b.setHead(TextLine.of(this.WAIT_TITLE));
                    b.setBody(this.getBody(gamePlayer));
                    scoreboards.put(gamePlayer, b);
                } else {
                    b = scoreboards.get(gamePlayer);
                    gamePlayer.getPlayer().setScoreboard(b.getScoreboard());
                    EffectStatsContainer effectStats = gamePlayer.getPlayerStats().getEffectStats();
                    b.setHead(TextLine.of(gamePlayer.getGameTeam() == null ? this.WAIT_TITLE : effectStats.getColor(gamePlayer.getGameTeam().getTeamColor()).getChatColor() + "§l超级战墙"));
                    b.setBody(this.getBody(gamePlayer));
                    b.update();
                }
            }

        }
    }

    private FixedBody getBody(GamePlayer gamePlayer) {
        List<xyz.yuzegod.megawalls.scoreboard.Line> lines = new ArrayList<xyz.yuzegod.megawalls.scoreboard.Line>();
        if (this.game.isWaiting()) {
            lines.add(TextLine.of(" "));
            lines.add(TextLine.of("§f地图：§a" + this.game.getMapName()));
            lines.add(TextLine.of("§f玩家：§a" + MegaWalls.getIngame().size() + "/" + this.game.getMaxPlayers()));
            lines.add(TextLine.of("  "));
            lines.add(TextLine.of("§f倒计时：§a" + this.game.getFormattedTime(this.game.getGameTimer().getTime())));
            lines.add(TextLine.of("§f需要玩家：§a" + (MegaWalls.getIngame().size() > this.game.getMinPlayers() ? 0 : this.game.getMinPlayers() - MegaWalls.getIngame().size())));
            GameParty party = this.game.getPlayerParty(gamePlayer);
            if (party != null && !party.getMates(gamePlayer).isEmpty()) {
                lines.add(TextLine.of("   "));
                lines.add(TextLine.of("§f队友："));

                for (GamePlayer mate : party.getMates(gamePlayer)) {
                    lines.add(TextLine.of("§a" + mate.getName()));
                }
            }

            lines.add(TextLine.of("    "));
            lines.add(TextLine.of("§7#" + this.dateFormat.format(new Date())));
            lines.add(TextLine.of("§emc.mooncookie.top"));
        } else if (this.game.isStarted()) {
            lines.add(TextLine.of(" "));
            lines.add(TextLine.of(this.game.getStageManager().formattedNextStage()));
            lines.add(TextLine.of("  "));
            String[] teamLines = this.game.getScoreboardTeamLines(gamePlayer);

            for (String teamLine : teamLines) {
                lines.add(TextLine.of(teamLine));
            }

            lines.add(TextLine.of("   "));
            lines.add(TextLine.of("§f击杀/淘汰：§a" + gamePlayer.getKills()));
            lines.add(TextLine.of("§f最终击杀/助攻：§a" + gamePlayer.getFinalKills()));
            lines.add(TextLine.of("    "));
            lines.add(TextLine.of("§7#" + this.dateFormat.format(new Date())));
            lines.add(TextLine.of("§emc.mooncookie.top"));
        }

        return FixedBody.of(lines);
    }
}
